package game;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

public class MainMenuPanel extends JPanel {
    private JButton startButton;
    private JButton exitButton;
    private GameWindow gameWindow;
    private BufferedImage background;
    public MainMenuPanel(GameWindow gameWindow) {
        this.gameWindow = gameWindow;
        setLayout(new GridBagLayout());
        setBackground(Color.BLACK);
        InputStream is = getClass().getResourceAsStream("/zhujiemian_bj.png");
        //background = ImageLoader.loadImage("resources/zhujiemian_bj.png");
        try {
            if (is != null) {
                background = ImageIO.read(is);
            }else {
                System.err.println("无法加载图片资源");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        JLabel titleLabel = new JLabel("拳皇97", SwingConstants.CENTER);
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 48));
        titleLabel.setForeground(Color.RED);

        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(2, 1, 10, 10));
        buttonPanel.setOpaque(false);

        startButton = new JButton("开始游戏");
        startButton.setFont(new Font("微软雅黑", Font.BOLD, 24));
        startButton.addActionListener(e -> gameWindow.switchToCharacterSelect());

        exitButton = new JButton("退出游戏");
        exitButton.setFont(new Font("微软雅黑", Font.BOLD, 24));
        exitButton.addActionListener(e -> System.exit(0));

        buttonPanel.add(startButton);
        buttonPanel.add(exitButton);

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbc.insets = new Insets(20, 0, 20, 0);

        add(titleLabel, gbc);
        add(buttonPanel, gbc);
    }
    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 绘制背景
        if (background != null) {
            g.drawImage(background, 0, 0, getWidth(), getHeight(), null);
        } else {
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, getWidth(), getHeight());
        }

        // 绘制其他组件
        super.paintChildren(g);
    }
}